
import http, { baseParams } from "@/utils/request";
import { UrlSaveEvent } from "@/utils/apiUrl";
import { toEventTagString } from "@/utils/utils";
import { getAreaInfo } from "@/utils/storage";
import { getZone, getZoneRealTime } from "./zone";
import { setZoneArea } from "./getZoneArea";

//类型：请求成功
export const SAVE_EVENT_REQ_SUCCESS = "SAVE_EVENT_REQ_SUCCESS"

//类型：请求失败
export const SAVE_EVENT_REQ_FAIL = "SAVE_EVENT_REQ_FAIL"

/**
 * 保存埋点
 * @returns thunk
 */
export const saveEvent = (type, remark = "") => {
    return async (dispatch) => {
        // 判断是否已经有path
        getZoneRealTime(async (result, zonePath) => {
            // 更新状态
            dispatch(setZoneArea(zonePath));

            console.log("========== saveEvent-" + zonePath)

            //组装参数
            const eventTypePrifix = process.env.REACT_APP_EVENT_PRIFIX + "";

            const params = baseParams()
            if (type.startsWith(eventTypePrifix)) {
                params.type = toEventTagString(type)
            } else {
                params.type = eventTypePrifix + toEventTagString(type)
            }
            params.remark = remark

            //发起请求
            await http.post(zonePath + UrlSaveEvent, params)
            console.log("保存埋点回调-结束：=======" + type)
        });
    }
}
